﻿package com.tudou.ad.view
{
    import com.tudou.ad.components.*;
    import com.tudou.ad.view.base.*;
    import com.tudou.home.consts.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class AdPlacardView extends AdFrontsViewBase
    {
        private var backgroundV1:DisplayObject;
        private var scale:Number = 1;
        protected var adDoor:AdDoor;
        private var backgroundV2:DisplayObject;

        public function AdPlacardView()
        {
            return;
        }// end function

        protected function clearBgv() : void
        {
            if (backgroundV1)
            {
                viewBox.removeChild(backgroundV1);
            }
            backgroundV1 = null;
            if (backgroundV2)
            {
                viewBox.removeChild(backgroundV2);
            }
            backgroundV2 = null;
            return;
        }// end function

        protected function startAdDoorClose() : void
        {
            adDoor.closeDoor();
            return;
        }// end function

        protected function openedDoorHlr(event:Event) : void
        {
            clearAdMian();
            return;
        }// end function

        override public function initToShow() : void
        {
            if (!initToShowCommon())
            {
                return;
            }
            hasFront = adItemInfo.url ? (true) : (false);
            if (mainHeight > 0)
            {
                scale = mainWidth / mainHeight;
            }
            initBgv();
            initFrontStuff();
            initControlPanel();
            addToShowStuff();
            etime = adItemInfo.durationType;
            return;
        }// end function

        private function startAdMian() : void
        {
            frontAdSprite.visible = true;
            if (timer)
            {
                if (adItemInfo.durationType <= 0)
                {
                    controlSprite.visible = false;
                }
                else
                {
                    controlSprite.visible = true;
                    timer.reset();
                    timer.start();
                }
            }
            return;
        }// end function

        override public function clearAd() : void
        {
            _clearAd(true);
            return;
        }// end function

        private function _clearAd(param1:Boolean) : void
        {
            if (!clearAdCommon())
            {
                return;
            }
            isSkip = param1;
            if (isSkip)
            {
                _adShowSkip();
            }
            if (adDoor)
            {
                frontAdSprite.visible = false;
                startAdDoorOpen();
            }
            else
            {
                clearAdMian();
            }
            return;
        }// end function

        protected function doorClickedHlr(event:MouseEvent) : void
        {
            adClick();
            return;
        }// end function

        override protected function adShowComplete() : void
        {
            if (!isSkip)
            {
                _adShowComplete();
            }
            _clearAd(isSkip);
            return;
        }// end function

        protected function startAdDoorOpen() : void
        {
            adDoor.openDoor();
            return;
        }// end function

        protected function initControlPanel() : void
        {
            controlSprite = new AdPlacardControlPanel();
            viewBox.addChild(controlSprite);
            controlSprite.width = mainWidth;
            controlSprite.visible = false;
            timer = new Timer(1000);
            timer.addEventListener(TimerEvent.TIMER, timerEventHlr);
            controlSprite.titleTime = _countDownTime - timer.currentCount;
            return;
        }// end function

        protected function timerEventHlr(event:TimerEvent) : void
        {
            var _loc_2:int = 0;
            _loc_2 = Math.max(0, int(_countDownTime) - timer.currentCount);
            controlSprite.titleTime = _loc_2;
            if (timer.currentCount >= adItemInfo.durationType)
            {
                adShowComplete();
            }
            return;
        }// end function

        protected function initBgv() : void
        {
            backgroundV1 = RectDisplayUtil.getShape(mainWidth, mainHeight, 0, 0.23);
            viewBox.addChild(backgroundV1);
            backgroundV2 = RectDisplayUtil.getShape(mainWidth, mainHeight - PlayerConsts.CONTROL_DEFAULT_HEIGTH, 0, 1);
            viewBox.addChild(backgroundV2);
            return;
        }// end function

        protected function clearControlPanel() : void
        {
            if (timer)
            {
                timer.removeEventListener(TimerEvent.TIMER, timerEventHlr);
            }
            timer = null;
            if (controlSprite)
            {
                controlSprite.clear();
                viewBox.removeChild(controlSprite);
            }
            controlSprite = null;
            return;
        }// end function

        override public function goTransform(param1:uint, param2:uint, param3:Boolean) : void
        {
            _isFullscreen = param3;
            var _loc_4:* = _isExpand ? (22) : (0);
            if (frontAdSprite)
            {
                frontAdSprite.y = _loc_4;
            }
            if (adDoor)
            {
                adDoor.y = _loc_4;
            }
            if (controlSprite)
            {
                controlSprite.y = 0;
                controlSprite.width = mainWidth;
            }
            if (backgroundV1)
            {
                backgroundV1.width = param1;
                backgroundV1.height = param2;
            }
            if (backgroundV2)
            {
                backgroundV2.width = param1;
                backgroundV2.height = param2 - PlayerConsts.CONTROL_DEFAULT_HEIGTH;
            }
            if (_isExpand)
            {
                if (frontAdSprite)
                {
                    frontAdSprite.setWidthHeight(param1, param1 / scale);
                }
                if (adDoor)
                {
                    adDoor.width = param1;
                    adDoor.height = param1 / scale;
                }
            }
            else
            {
                if (frontAdSprite)
                {
                    frontAdSprite.setWidthHeight(param1, param2);
                }
                if (adDoor)
                {
                    adDoor.width = param1;
                    adDoor.height = param2;
                }
            }
            return;
        }// end function

        override public function startAd() : void
        {
            if (!startAdCommon())
            {
                return;
            }
            showAd();
            if (adDoor)
            {
                frontAdSprite.visible = false;
                startAdDoorClose();
            }
            else
            {
                startAdMian();
            }
            adShowStart();
            return;
        }// end function

        protected function initAdDoor() : void
        {
            adDoor = new AdDoor(Bitmap(frontStuff).bitmapData);
            viewBox.addChild(adDoor);
            adDoor.addEventListener(AdDoor.CLOSE_SHOWED, closedDoorHlr);
            adDoor.addEventListener(AdDoor.OPEN_SHOWED, openedDoorHlr);
            adDoor.addEventListener(MouseEvent.CLICK, doorClickedHlr);
            return;
        }// end function

        protected function clearAdDoor() : void
        {
            if (adDoor)
            {
                adDoor.removeEventListener(AdDoor.CLOSE_SHOWED, closedDoorHlr);
                adDoor.removeEventListener(AdDoor.OPEN_SHOWED, openedDoorHlr);
                adDoor.removeEventListener(MouseEvent.CLICK, doorClickedHlr);
                viewBox.removeChild(adDoor);
            }
            adDoor = null;
            return;
        }// end function

        protected function closedDoorHlr(event:Event) : void
        {
            startAdMian();
            return;
        }// end function

        private function clearAdMian() : void
        {
            hideAd();
            clearComps();
            countDownTime = 0;
            adItemInfo = null;
            adShowEnd();
            return;
        }// end function

        override protected function clearComps() : void
        {
            clearControlPanel();
            clearBgv();
            clearFrontStuff();
            clearAdDoor();
            clearCompsCommon();
            return;
        }// end function

    }
}
